package com.lvmama.utils;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.util.*;

public class ToolUtils {

    /**
     * @Title: getQueryDate
     * @Description: 查询条件起始时间的处理
     * @param @param choose_date
     * @param @return 设定文件
     * @return Map<String,Date> 返回类型
     * @throws
     */
    public static Map<String, Date> getQueryDate(String choose_date) {
        Map<String, Date> map = new HashMap<>();
        DateTimeFormatter formater = DateTimeFormat.forPattern("MM/dd/yyyy HH:mm:ss");
        Date beginDate = DateTime.parse(choose_date.split("-")[0].trim() + " 00:00:00", formater).toDate();
        Date endDate = DateTime.parse(choose_date.split("-")[1].trim() + " 23:59:59", formater).toDate();
        map.put("beginDate", beginDate);
        map.put("endDate", endDate);
        return map;
    }

    /**
     * @Title: sortByValueDesc
     * @Description: map降序排序
     * @param @param map
     * @param @return 设定文件
     * @return Map<K,V> 返回类型
     * @throws
     */
    public static <K, V extends Comparable<? super V>> Map<K, V> sortByValueDesc(
            Map<K, V> map) {
        List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(
                map.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
            public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
                return (o2.getValue()).compareTo(o1.getValue());
            }
        });
        Map<K, V> result = new LinkedHashMap<K, V>();
        for (Map.Entry<K, V> entry : list) {
            result.put(entry.getKey(), entry.getValue());
        }
        return result;
    }

}
